package net.xt.testthread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;

public class TestClickNumber {

    // 使用synchronized
    int num = 0;

    public synchronized void clickBySynchronized() {
        num++;
    }

    // 使用AtomicInteger
    AtomicInteger atomicInteger = new AtomicInteger(0);

    public void clcikByAtomicInteger() {
        atomicInteger.getAndIncrement();
    }

    // 使用AtomicInteger
    LongAdder longAdder = new LongAdder();

    public void clickByLongAdder() {
        longAdder.increment();
    }

    // 使用LongAccumulator
    LongAccumulator longAccumulator = new LongAccumulator((x, y) -> {
        return x + y;
    }, 0);

    public void clickByLongAccumulator() {
        longAccumulator.accumulate(1);
    }

    // 测试
    public static void main(String[] args) throws InterruptedException {
        TestClickNumber testClickNumber = new TestClickNumber();

        long startTime = 0;
        long endTime = 0;

        CountDownLatch countDownLatch1 = new CountDownLatch(50);
        CountDownLatch countDownLatch2 = new CountDownLatch(50);
        CountDownLatch countDownLatch3 = new CountDownLatch(50);
        CountDownLatch countDownLatch4 = new CountDownLatch(50);

        startTime = System.currentTimeMillis();
        // 启动50个线程
        for (int i = 0; i < 50; i++) {
            new Thread(() -> {
                // 执行100*10000次
                try {
                    for (int j = 0; j < 100 * 10000; j++) {
                        testClickNumber.clickBySynchronized();
                    }
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                finally {
                    countDownLatch1.countDown();
                }
            }, "线程" + String.valueOf(i)).start();
        }
        countDownLatch1.await();
        endTime = System.currentTimeMillis();
        System.out.println("clickBySynchronized用时：" + (endTime - startTime) + "\tnum=" + testClickNumber.num);

        startTime = System.currentTimeMillis();
        // 启动50个线程
        for (int i = 0; i < 50; i++) {
            new Thread(() -> {
                // 执行100*10000次
                try {
                    for (int j = 0; j < 100 * 10000; j++) {
                        testClickNumber.clcikByAtomicInteger();
                    }
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                finally {
                    countDownLatch2.countDown();
                }
            }, "线程" + String.valueOf(i)).start();
        }
        countDownLatch2.await();
        endTime = System.currentTimeMillis();
        System.out.println("clcikByAtomicInteger用时：" + (endTime - startTime) + "\tnum=" + testClickNumber.atomicInteger.get());


        startTime = System.currentTimeMillis();
        // 启动50个线程
        for (int i = 0; i < 50; i++) {
            new Thread(() -> {
                // 执行100*10000次
                try {
                    for (int j = 0; j < 100 * 10000; j++) {
                        testClickNumber.clickByLongAdder();
                    }
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                finally {
                    countDownLatch3.countDown();
                }
            }, "线程" + String.valueOf(i)).start();
        }
        countDownLatch3.await();
        endTime = System.currentTimeMillis();
        System.out.println("clickByLongAdder用时：" + (endTime - startTime) + "\tnum=" + testClickNumber.longAdder.sum());


        startTime = System.currentTimeMillis();
        // 启动50个线程
        for (int i = 0; i < 50; i++) {
            new Thread(() -> {
                // 执行100*10000次
                try {
                    for (int j = 0; j < 100 * 10000; j++) {
                        testClickNumber.clickByLongAccumulator();
                    }
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                finally {
                    countDownLatch4.countDown();
                }
            }, "线程" + String.valueOf(i)).start();
        }
        countDownLatch4.await();
        endTime = System.currentTimeMillis();
        System.out.println("clickByLongAccumulator用时：" + (endTime - startTime) + "\tnum=" + testClickNumber.longAccumulator.get());
    }

}
